package com.test.demo;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author DouWeiWei
 * @createTime 2020年07月31日 14:05:00
 */
public class GreetingClient {
    public static void main(String[] args) {
        try {
            String serverName = args[0];
            int port = Integer.parseInt(args[1]);
            System.out.println("连接到主机：" + serverName + "，端口号为：" + port);
            Socket client = new Socket(serverName, port);
            System.out.println("远程主机是：" + client.getRemoteSocketAddress());
            OutputStream outputServer = client.getOutputStream();
            DataOutputStream out = new DataOutputStream(outputServer);

            out.writeUTF("hello from" + client.getLocalSocketAddress());
            InputStream inFromServer = client.getInputStream();
            DataInputStream in = new DataInputStream(inFromServer);
            System.out.println("服务器响应" + in.readUTF());
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
